package kibahed.schnapsen.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import kibahed.client.SchnapsClient;
import kibahed.interfaces.cards.Card;
import kibahed.interfaces.cards.CardFactory;
import kibahed.interfaces.cards.ECardValue;
import kibahed.interfaces.cards.EGame;
import kibahed.interfaces.cards.ESuite;
import kibahed.schnapsen.Bildkarte;

public class SchnapsTisch extends JPanel implements ISchnapsTable {

	SchnapsClient client = null;

	
	private static int multiplikator = 2;
	boolean myTurn = false;

	Set<Bildkarte> myHand = new HashSet<Bildkarte>();
	Set<Bildkarte> yourHand = new HashSet<Bildkarte>();
	Bildkarte meineAusgespielteKarte = null;
	Bildkarte deineAusgespielteKarte = null;
	Set<Bildkarte> meineStiche = new HashSet<Bildkarte>();
	Set<Bildkarte> deineStiche = new HashSet<Bildkarte>();
	Bildkarte adutt = null;
	private boolean showMessages=true;
	private int messageCounter = 0;
	private boolean zugedreht = false;
	int counter = 0;

	private int myPoints;
	private int yourPoints;

	SchnapsTischProperties properties = new SchnapsTischProperties();
	
	private JPanel mySide = new JPanel();
	private JPanel yourSide = new JPanel();
	private JPanel mainPanel = new JPanel();
	private JPanel topPanel = new JPanel();
	private JPanel myCardPanel = new JPanel();
	private JPanel yourCardPanel = new JPanel();
	private JPanel table = new JPanel();
	private JPanel deckPanel = new JPanel();
	private JPanel adutPanel = new JPanel();
	private JPanel meinStichPanel = new JPanel();
	private JPanel deinStichPanel = new JPanel();
	private JPanel myCard = new JPanel();
	private JPanel yourCard = new JPanel();
	private JPanel yourPairs = new JPanel();
	private JPanel myPairs = new JPanel();
	private JLabel myPointsLabel = new JLabel("0");
	private JLabel yourPointsLabel = new JLabel("0");
	private JPanel myFirstStich = new JPanel();
	private JPanel yourFirstStich = new JPanel();
	private JPanel myStiche = new JPanel();
	private JPanel yourStiche = new JPanel();
	private JPanel gestocheneKarten = new JPanel();
	Color tableColor = new Color(60, 120, 80, 255);
	Color cardSpaceColor = new Color(255, 255, 255, 50);

	JLabel ipDescription = new JLabel("server ip:");
	JLabel portDescription = new JLabel("port");
	JTextField ipField = new JTextField(15);
	JTextField portField = new JTextField(4);
	JButton connectButton = new JButton("connect");
	JButton newRoundButton = new JButton("neue Runde");
	JButton neuesBummerButton = new JButton("neues Bummerl");
	JButton sucheSpielButton = new JButton("Suche Spiel");

	public void resetBummerl() {
		myPoints = 7;
		yourPoints = 7;
		myPointsLabel.setText("" + myPoints);
		yourPointsLabel.setText("" + yourPoints);
		myPointsLabel.setVisible(true);
		yourPointsLabel.setVisible(true);
		validate();
		updateUI();
	}

	public void reset() {
		this.removeAll();
		mainPanel.removeAll();
		table.removeAll();
		yourPairs.removeAll();
		myPairs.removeAll();
		myCard.removeAll();
		yourCard.removeAll();
		deckPanel.removeAll();
		adutPanel.removeAll();
		topPanel.removeAll();
		mySide.removeAll();
		yourSide.removeAll();
		meinStichPanel.removeAll();
		deinStichPanel.removeAll();
		gestocheneKarten.removeAll();
		myCardPanel.removeAll();
		yourCardPanel.removeAll();
		myFirstStich.removeAll();
		yourFirstStich.removeAll();
		myStiche.removeAll();
		yourStiche.removeAll();
		myHand.clear();
		yourHand.clear();
		meineStiche.clear();
		deineStiche.clear();
		meineAusgespielteKarte = null;
		deineAusgespielteKarte = null;
		adutt = null;
		counter = 0;
		zugedreht = false;
		this.removeAll();
		setLayout();
		addPanels();
		setPositions();
		evaluateDesktopSize();
		validate();
		updateUI();
	}

	public static void main(String[] args) {
		SchnapsTisch tisch = new SchnapsTisch();
		JFrame frame = new JFrame();
		//frame.setLayout(null);
		frame.add(tisch);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(0, 0,tisch.properties.getBreite(), tisch.properties.getHoehe()+tisch.properties.getButtonPanelHoehe());

//		 for (int i = 0; i < 5; i++) {
//		 Card card = CardFactory.getCardDeck(EGame.SCHNAPSEN).get(
//		 (int) (Math.random() * CardFactory.getCardDeck(
//		 EGame.SCHNAPSEN).size()));
//		 Bildkarte karte = new Bildkarte(card);
//		 tisch.addCardToMyHand(karte);
//		 }
//		 tisch.myTurn=true;
	}

	public SchnapsTisch() {
		super();
		setLayout();
		addPanels();
		setPositions();
		evaluateDesktopSize();
		
		

	}

	private void setLayout() {
		//this.setBounds(0, 0, 5000, 5000);
		this.setLayout(new BorderLayout());
		FlowLayout flowLayout = new FlowLayout();
		flowLayout.setAlignOnBaseline(true);
		
		mainPanel.setLayout(new BorderLayout());
		topPanel.setLayout(flowLayout);
		gestocheneKarten.setLayout(new GridLayout(0, 2));
		
		//meinFriedhof.setOpaque(false);
		gestocheneKarten.setBackground(tableColor);
		gestocheneKarten.setVisible(false);
		yourCardPanel.setOpaque(true);
		yourCardPanel.setLayout(flowLayout);
		
		yourCardPanel.setBackground(cardSpaceColor);
		myCardPanel.setOpaque(true);
		myCardPanel.setLayout(flowLayout);
		myCardPanel.setBackground(cardSpaceColor);
		
		table.setLayout(null);
		meinStichPanel.setLayout(flowLayout);
		deinStichPanel.setLayout(flowLayout);

		
		// table.setPreferredSize(new Dimension(200,200));
		table.setOpaque(false);
		mainPanel.setOpaque(true);
		mainPanel.setBackground(tableColor);
		yourPairs.setLayout(flowLayout);
		myPairs.setLayout(flowLayout);
		mySide.setLayout(flowLayout);
		yourSide.setLayout(flowLayout);
		myFirstStich.setLayout(flowLayout);
		yourFirstStich.setLayout(flowLayout);
		myStiche.setLayout(flowLayout);
		yourStiche.setLayout(flowLayout);

	}
	
	private void setPositions(){
		topPanel.setPreferredSize(new Dimension(0,properties.getButtonPanelHoehe()));
		yourCardPanel
		.setPreferredSize(new Dimension(
				5*properties.getIncreasedCardWidth(),
				properties.getIncreasedCardHeight()));
		myCardPanel
		.setPreferredSize(new Dimension(
				5*properties.getIncreasedCardWidth(),
				properties.getIncreasedCardHeight()));
		table.setPreferredSize(new Dimension( properties.getBreite(), properties.getTableHeight()));
		
		deckPanel.setBounds(properties.getDeckPosX(), (properties.getTableHeight()-properties.getKartenHoehe())/2,
				properties.getKartenBreite(), properties.getKartenHoehe());
		adutPanel.setBounds(properties.getAdutPosX(), (properties.getTableHeight()-properties.getIncreasedCardHeight())/2,
				properties.getIncreasedCardWidth(), properties.getIncreasedCardHeight());
		yourCard.setBounds(properties.getPlayedCardPosX(), properties.getStichPanelPosY(), properties.getKartenBreite(),
				properties.getKartenHoehe());
		myCard.setBounds(properties.getPlayedCardPosX(),properties.getTableHeight()-properties.getStichPanelPosY()-properties.getKartenHoehe(), properties.getKartenBreite(),
				properties.getKartenHoehe());
		deinStichPanel.setBounds(properties.getStichPanelPosX(),properties.getStichPanelPosY(),
				(int)(2*properties.getIncreasedCardWidth()), properties.getKartenHoehe());
		meinStichPanel.setBounds(properties.getStichPanelPosX(),properties.getTableHeight()-properties.getStichPanelPosY()-properties.getKartenHoehe(),
				(int)(2*properties.getIncreasedCardWidth()), properties.getKartenHoehe());
		yourPairs.setBounds(0, 0, properties.getPairWidth(), properties.getPairHeight());

		myPairs.setBounds(0, properties.getTableHeight()-properties.getPairHeight(), properties.getPairWidth(), properties.getPairHeight());
		myFirstStich.setBounds(properties.getFirstStichPosX(), properties.getTableHeight()-properties.getStichPanelPosY()-properties.getKartenHoehe(), 2*properties.getIncreasedCardWidth(), properties.getKartenHoehe());
		yourFirstStich.setBounds(properties.getFirstStichPosX(), properties.getStichPanelPosY(), 2*properties.getIncreasedCardWidth(), properties.getKartenHoehe());
		myStiche.setBounds(properties.getStichePosX(), properties.getTableHeight()-properties.getStichPanelPosY()-properties.getKartenHoehe(), properties.getKartenBreite(), properties.getKartenHoehe());
		yourStiche.setBounds(properties.getStichePosX(), properties.getStichPanelPosY(), properties.getKartenBreite(), properties.getKartenHoehe());
		gestocheneKarten.setPreferredSize(new Dimension(2*properties.getIncreasedCardWidth(),0));
		Bildkarte.setGlobalMultipier(properties.getKartenHoehe()/91.);
		Bildkarte.updateSizeAllCards();
	}

	private void addPanels() {

		// this.add(topPanel,BorderLayout.PAGE_START);

		this.add(topPanel, BorderLayout.NORTH);
		this.add(mainPanel, BorderLayout.CENTER);
		this.add(gestocheneKarten,BorderLayout.EAST);
		
		mainPanel.add(yourSide, BorderLayout.NORTH);
		mainPanel.add(table, BorderLayout.CENTER);
		mainPanel.add(mySide, BorderLayout.SOUTH);
		

		table.add(deckPanel);

		topPanel.add(ipDescription);
		topPanel.add(ipField);
		topPanel.add(portDescription);
		topPanel.add(portField);
		topPanel.add(connectButton);
		if (connectButton.getActionListeners().length == 0) {
			connectButton.addActionListener(getConnectListener());
		}
		topPanel.add(sucheSpielButton);
		if (sucheSpielButton.getActionListeners().length == 0) {
			sucheSpielButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					showText("Suche nach Gegner");
					client.sucheSpiel();

				}
			});
		}
		this.addComponentListener(new ComponentAdapter() {
			
			
			
			@Override
			public void componentResized(ComponentEvent arg0) {
				properties.setBreite(SchnapsTisch.this.getWidth());
				properties.setHoehe((int)(SchnapsTisch.this.getHeight()));
				myCardPanel.updateUI();
				yourCardPanel.updateUI();
				
				setPositions();
				
				validate();
				updateUI();
			}
			
		});
		topPanel.add(newRoundButton);
		if (newRoundButton.getActionListeners().length == 0) {
			newRoundButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					showText("Neue Runde");
					client.restart();

				}
			});
		}
		topPanel.add(neuesBummerButton);
		if (neuesBummerButton.getActionListeners().length == 0) {
			neuesBummerButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					showText("Neues Bummerl");
					client.neuesBummerl();

				}
			});
		}
		if (ipField.getText().isEmpty()) {
			ipField.setText("localhost");
		}
		if (portField.getText().isEmpty()) {
			portField.setText("5555");
		}
		
		

		mySide.setOpaque(false);
		yourSide.setOpaque(false);

		myPointsLabel.setPreferredSize(new Dimension(properties.getIncreasedCardHeight(), 0));
		yourPointsLabel.setPreferredSize(new Dimension(properties.getIncreasedCardHeight(), 0));

		myPointsLabel.setFont(new Font("Arial", Font.BOLD, properties.getIncreasedCardHeight()/3));
		myPointsLabel.setHorizontalTextPosition(JLabel.CENTER);
		myPointsLabel.setVerticalTextPosition(JLabel.CENTER);
		yourPointsLabel.setFont(new Font("Arial", Font.BOLD, properties.getIncreasedCardHeight()/3));
		yourPointsLabel.setHorizontalTextPosition(JLabel.CENTER);
		yourPointsLabel.setVerticalTextPosition(JLabel.CENTER);
		
		mySide.add(myPointsLabel, BorderLayout.WEST);
		yourSide.add(yourPointsLabel, BorderLayout.WEST);

		mySide.add(myCardPanel, BorderLayout.CENTER);
		yourSide.add(yourCardPanel, BorderLayout.CENTER);

		
		deckPanel.setOpaque(true);
		deckPanel.setBackground(cardSpaceColor);
		deckPanel.add(Bildkarte.getUmgedrehteKarte());
		table.add(adutPanel);
		adutPanel.setOpaque(true);
		adutPanel.setBackground(cardSpaceColor);

		table.add(yourCard);

		
		yourCard.setOpaque(true);
		yourCard.setBackground(cardSpaceColor);

		meinStichPanel.setOpaque(true);
		deinStichPanel.setOpaque(true);
		meinStichPanel.setBackground(cardSpaceColor);

		deinStichPanel.setBackground(cardSpaceColor);
		table.add(myCard);
	
		
		meinStichPanel.setOpaque(true);
		deinStichPanel.setOpaque(true);
		table.add(meinStichPanel);
		table.add(deinStichPanel);
		
		myStiche.setOpaque(true);
		yourStiche.setOpaque(true);
		myFirstStich.setOpaque(true);
		yourFirstStich.setOpaque(true);
		myStiche.setBackground(cardSpaceColor);
		yourStiche.setBackground(cardSpaceColor);
		myFirstStich.setBackground(cardSpaceColor);
		yourFirstStich.setBackground(cardSpaceColor);
		table.add(myStiche);
		table.add(yourStiche);
		table.add(myFirstStich);
		table.add(yourFirstStich);
		
		myCard.setOpaque(true);
		myCard.setBackground(cardSpaceColor);

		
		table.add(myPairs);
		table.add(yourPairs);

	}

	public ActionListener getConnectListener() {
		ActionListener connectListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				showText("connecting");
				try {
					String ip = ipField.getText();
					int port = Integer.parseInt(portField.getText());
					SchnapsTisch.this.client = new SchnapsClient(ip, port,
							SchnapsTisch.this);

				} catch (Exception e) {

				}

			}
		};

		return connectListener;
	}

	

	private void evaluateDesktopSize() {

	}

	public void addCardToMyHand(final String string) {
		final Bildkarte karte=Bildkarte.getBildkarte(string);
		
		addHighLighter(karte);
		counter++;
		if (counter == 5) {
			adutPanel.removeAll();
		}
		karte.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseReleased(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) {
					client.playCard(karte.getCard().getTag(""));
				} else if (e.getButton() == MouseEvent.BUTTON3) {
					if (karte.getCard().getCardValue().equals(ECardValue.DAME)
							|| karte.getCard().getCardValue()
									.equals(ECardValue.KOENIG)) {
						client.sagAn(karte.getCard().getTag("/"));
					} else if (karte.getCard().getCardValue()
							.equals(ECardValue.BUB)) {
						client.tauscheAus(karte.getCard().getTag("/"));
					}
				}
			}

		});

		myHand.add(karte);
		myCardPanel.add(karte);
		yourCardPanel.add(Bildkarte.getUmgedrehteKarte());
		validate();
		updateUI();
	}

	public void addCardToYourHand(String string) {
		Bildkarte karte=Bildkarte.getBildkarte(string);
		yourHand.add(karte);
		yourCardPanel.add(karte);
		validate();
		updateUI();
	}

	public void iPlayCard(String string) {
		Bildkarte karte=Bildkarte.getBildkarte(string);
		for (MouseListener listener : karte.getMouseListeners()) {
			karte.removeMouseListener(listener);
		}
		if (karte.getSizeMultipier() != 1) {
			karte.setSizeMultipier(1);
			karte.updateImage();
		}

		meineAusgespielteKarte = karte;
		myHand.remove(karte);
		myCardPanel.remove(karte);
		myCard.add(karte);
		addCardsToStiche();
		
		myTurn=false;
		
		validate();
		updateUI();
		
	}

	private void addCardsToStiche() {
		for(Component c:meinStichPanel.getComponents()){
			if(myFirstStich.getComponents().length<2){
				myFirstStich.add(c);
			}else if(myStiche.getComponents().length==0){
				if(c instanceof Bildkarte){
					Bildkarte karte = Bildkarte.getUmgedrehteKarte();
					karte.addMouseListener(new MouseAdapter() {

						@Override
						public void mouseEntered(MouseEvent e) {
							gestocheneKarten.setVisible(true);
							validate();
							updateUI();
						}

						@Override
						public void mouseExited(MouseEvent e) {
							gestocheneKarten.setVisible(false);
							validate();
							updateUI();
						}
						
						
					});
					myStiche.add(karte);
					gestocheneKarten.add(c);
				}
			}else{
				gestocheneKarten.add(c);
			}
		}
		
		for(Component c:deinStichPanel.getComponents()){
			if(yourFirstStich.getComponents().length<2){
				yourFirstStich.add(c);
			}else if(yourStiche.getComponents().length==0){
				if(c instanceof Bildkarte){
					yourStiche.add(Bildkarte.getUmgedrehteKarte());
				}
			}else{
				break;
			}
		}
		meinStichPanel.removeAll();
		deinStichPanel.removeAll();
		
	}

	public void youPlayCard(String string) {
		Bildkarte karte=Bildkarte.getBildkarte(string);
		if (karte.getSizeMultipier() != 1) {
			karte.setSizeMultipier(1);
			karte.updateImage();
		}
		deineAusgespielteKarte = karte;
		yourCard.add(karte);
		addCardsToStiche();
		yourCardPanel.remove(yourCardPanel.getComponent(0));
		validate();
		updateUI();
	}

	public void setAdut(String string) {
		Bildkarte karte=Bildkarte.getBildkarte(string);
		Bildkarte cardFromHand = null;
		for (Bildkarte card : myHand) {
			if (card.getCard().equals(karte.getCard())) {
				cardFromHand = card;
				yourCardPanel.remove(yourCardPanel.getComponent(0));
				counter--;
			}
		}
		if (cardFromHand != null) {
			myHand.remove(cardFromHand);
			myCardPanel.remove(cardFromHand);
			karte = cardFromHand;
			karte.setSizeMultipier(1);
			karte.updateImage();
		}
		adutPanel.removeAll();
		adutt = karte;

		adutt.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseReleased(MouseEvent e) {
				client.dreheZu();
			}

		});
		showText("Adut ist "+karte.getCard().getTag("-"));

		adutPanel.add(karte);
		addHighLighter(karte);
		validate();
		updateUI();
	}

	public void meinStrich() {
		meineStiche.add(meineAusgespielteKarte);
		meineStiche.add(deineAusgespielteKarte);
		myCard.removeAll();
		yourCard.removeAll();

		meinStichPanel.add(meineAusgespielteKarte);
		meinStichPanel.add(deineAusgespielteKarte);
		meineAusgespielteKarte = null;
		deineAusgespielteKarte = null;
		showMessages=false;
		validate();
		updateUI();
	}

	public void deinStrich() {
		deineStiche.add(meineAusgespielteKarte);
		deineStiche.add(deineAusgespielteKarte);
		yourCard.removeAll();
		myCard.removeAll();

		deinStichPanel.add(meineAusgespielteKarte);
		deinStichPanel.add(deineAusgespielteKarte);
		meineAusgespielteKarte = null;
		deineAusgespielteKarte = null;
		showMessages=false;
		validate();
		updateUI();
	}

	public Set<String> getMyHand() {
		Set<String> handCards = new HashSet<String>();
		for(Bildkarte karte:myHand){
			handCards.add(karte.getCard().getTag(""));
		}
		return handCards;
	}

	public void addHighLighter(final Bildkarte karte) {

		MouseAdapter highlighter = new MouseAdapter() {
			long counter = 0;
			boolean isResizing = false;

			@Override
			public void mouseEntered(final MouseEvent e) {
				if (myTurn) {
					counter++;
					final long currentValue = counter;
					Thread th = new Thread() {

						public synchronized void run() {
							while (isResizing) {
								try {
									Thread.sleep(10);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if (currentValue != counter) {
									isResizing = false;
									return;
								}

							}
							isResizing = true;

							int x = 100;
							while (x < (int)(100*properties.getMultiplier()*0.9)) {
								x += 1;

								karte.setSizeMultipier(x / 100.);
								karte.updateImage();

								mySide.validate();
								mySide.updateUI();
								try {
									Thread.sleep(10);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if (currentValue != counter) {
									isResizing = false;
									return;
								}
							}
							isResizing = false;

						}

					};
					th.start();
				}

			}

			@Override
			public void mouseExited(MouseEvent e) {
				if(myTurn){
				counter++;
				
				final long currentValue = counter;
				Thread th = new Thread() {

					public synchronized void run() {

						int x = (int) (100*properties.getMultiplier()*0.9);
						while (isResizing) {
							try {
								Thread.sleep(10);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							if (currentValue != counter) {
								isResizing = false;
								return;
							}
						}
						isResizing = true;
						while (x > 100) {
							x -= 1;

							karte.setSizeMultipier(x / 100.);
							karte.updateImage();

							mySide.validate();
							mySide.updateUI();
							try {
								Thread.sleep(10);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							if (currentValue != counter) {
								isResizing = false;
								return;
							}
						}
						isResizing = false;

					}

				};
				th.start();
				}else{
					karte.setSizeMultipier(1);
				}
			}

		};
		karte.addMouseListener(highlighter);

	}

	public void setZugedreht(boolean flag) {
		this.zugedreht = flag;
		adutPanel.removeAll();
		adutPanel.add(Bildkarte.getUmgedrehteKarte());
		showText("Es wurde zugedreht!");
		validate();
		updateUI();
	}

	public void youSayPair(ESuite suite) {
		System.out.println("You say pair");
		ImageIcon icon = new ImageIcon(String.format("symbole/%s.png", suite
				.toString().toLowerCase()));
		icon = new ImageIcon(icon.getImage().getScaledInstance(properties.getPairHeight(), properties.getPairHeight(),
				Image.SCALE_SMOOTH));
		System.out.println(String.format("icon = %s", icon));
		JLabel label = new JLabel(icon);
		yourPairs.add(label);
		
		if(suite.equals(adutt.getCard().getSuite())){
			showText("Dein Gegner sagt 40!");
		}else{
			showText("Dein Gegner sagt 20!");
		}
		
		validate();
		updateUI();

	}

	public void iSayPair(ESuite suite) {
		System.out.println("i say Pair");
		ImageIcon icon = new ImageIcon(String.format("symbole/%s.png", suite
				.toString().toLowerCase()));
		icon = new ImageIcon(icon.getImage().getScaledInstance(properties.getPairHeight(), properties.getPairHeight(),
				Image.SCALE_SMOOTH));
		System.out.println(String.format("icon = %s", icon));
		JLabel label = new JLabel(icon);
		myPairs.add(label);
		myTurn=false;
		
		if(suite.equals(adutt.getCard().getSuite())){
			showText("Ich sage 40!");
		}else{
			showText("Ich sage 20!");
		}
		
		validate();
		updateUI();

	}

	public void reduceMyPoints(int d) {
		myPoints -= d;
		myPointsLabel.setText("" + myPoints);
		myPointsLabel.setVisible(true);
	}

	public void reduceYourPoints(int d) {
		yourPoints -= d;
		yourPointsLabel.setText("" + yourPoints);
		yourPointsLabel.setVisible(true);

	}

	public void removeStapel() {
		deckPanel.removeAll();
		adutPanel.removeAll();
	}

	public void myTurn() {
		myTurn = true;
	}
	
	public void showText(final String message){
		
		showMessages=true;
		messageCounter++;
		
		Thread th = new Thread(){
			
			@Override
			public void run() {
				int messageNumber=messageCounter;
				JLabel label = new JLabel(message);
				label.setFont(new Font("Arial",Font.BOLD,properties.getKartenHoehe()));
				label.setHorizontalTextPosition(JLabel.CENTER);
				label.setVerticalTextPosition(JLabel.CENTER);
				label.setSize(properties.getBreite(), properties.getKartenHoehe());
				int startx = -properties.getBreite();
				int starty = properties.getTableHeight()/2-properties.getKartenHoehe()/2;
				int endx=properties.getBreite();
				int endy=starty;
				table.add(label);
				int x=endx;
				int y=endy;
				int ratiox = (endx-startx)/100;
				int ratioy=(endy-starty)/100;
				while(showMessages&&messageCounter==messageNumber){
					if(x<=startx&&y<=starty){
						x=endx;
						y=endy;
					}
					label.setLocation(x, y);
					x-=ratiox;
					y-=ratioy;
					table.validate();
					table.updateUI();
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				table.remove(label);
				table.validate();
				table.updateUI();
				showMessages=true;
				
			}
			
		};
		th.start();
	}

	@Override
	public void setClient(SchnapsClient client) {
		// TODO Auto-generated method stub
		
	}
}
